<template>
  <div class="about">
    <h1>登录or注册</h1>

    <div>
      <div>
        <input type="text" name="" id="" v-model="loginname" />
        <input type="text" name="" id="" v-model="password" />
        <br />
        <button @click="loginFn">登录</button>
        <button @click="regFn">注册</button>
      </div>
    </div>
  </div>
</template>
<script setup>
import router from '@/router'
import http from '@/utils/http'

import { ref } from 'vue'
let showIndex = ref(0)
let loginname = ref('')
let password = ref('')

function loginFn() {
  http({
    url: '/api/user/login',
    method: 'post',
    data: { loginname: loginname.value, password: password.value },
  }).then((r) => {
    console.log(r.data)
    localStorage.setItem('homework-token', r.data.data.token)
    localStorage.setItem('homework-userid', r.data.data.userid)
    localStorage.setItem('homework-usename', loginname.value)
    alert(r.data.message)
    router.push('/')
  })
}
function regFn() {
  http({
    url: '/api/user/reg',
    method: 'post',
    data: { loginname: loginname.value, password: password.value },
  }).then((r) => {
    console.log(r.data)
    alert(r.data.message)
  })
}
</script>

<style></style>
